<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Connecting to MySQL databases</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ref.pdo-mysql.html">MySQL (PDO)</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.pdo-sqlsrv.html">MS SQL Server (PDO)</a></div>
 <div class="up"><a href="ref.pdo-mysql.html">MySQL (PDO)</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="ref.pdo-mysql.connection" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">PDO_MYSQL DSN</h1>
    <p class="verinfo">(PECL PDO_MYSQL &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">PDO_MYSQL DSN</span> &mdash; <span class="dc-title">Connecting to MySQL databases</span></p>

   </div>

   <div class="refsect1 description" id="refsect1-ref.pdo-mysql.connection-description">
    <h3 class="title">说明</h3>
    <p class="para">
     The PDO_MYSQL Data Source Name (DSN) is composed of the following elements:
     <dl>

      
       <dt>
DSN prefix</dt>

       <dd>

        <p class="para">
        The DSN prefix is <strong class="userinput"><code>mysql:</code></strong>.
        </p>
       </dd>

      
      
       <dt>
<em>host</em></dt>

       <dd>

        <p class="para">
         The hostname on which the database server resides.
        </p>
       </dd>

      
      
       <dt>
<em>port</em></dt>

       <dd>

        <p class="para">
         The port number where the database server is listening.
        </p>
       </dd>

      
      
       <dt>
<em>dbname</em></dt>

       <dd>

        <p class="para">
         The name of the database.
        </p>
       </dd>

      
      
       <dt>
<em>unix_socket</em></dt>

       <dd>

        <p class="para">
         The MySQL Unix socket (shouldn&#039;t be used with <em>host</em>
         or <em>port</em>).
        </p>
       </dd>

      
      
       <dt>
<em>charset</em></dt>

       <dd>

        <p class="para">
         The character set. See the <a href="mysqlinfo.concepts.charset.html" class="link">character set</a>
         concepts documentation for more information.
        </p>
        <p class="para">
         Prior to PHP 5.3.6, this element was silently ignored. The same
         behaviour can be partly replicated with the
         <strong><code>PDO::MYSQL_ATTR_INIT_COMMAND</code></strong> driver option, as
         the following example shows.
        </p>
        <div class="warning"><strong class="warning">Warning</strong>
         <p class="simpara">
          The method in the below example can only be used with character sets
          that share the same lower 7 bit representation as ASCII, such as
          ISO-8859-1 and UTF-8. Users using character sets that have different
          representations (such as UTF-16 or Big5) <em class="emphasis">must</em>
          use the <em>charset</em> option provided in PHP 5.3.6
          and later versions.
         </p>
        </div>
        <p class="para">
         <div class="example" id="example-952">
          <p><strong>Example #1 
           Setting the connection character set to UTF-8 prior to PHP 5.3.6
          </strong></p>
          <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dsn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'mysql:host=localhost;dbname=testdb'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$username&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'username'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'password'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">MYSQL_ATTR_INIT_COMMAND&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'SET&nbsp;NAMES&nbsp;utf8'</span><span style="color: #007700">,<br />);&nbsp;<br /><br /></span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$username</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
          </div>

         </div>
        </p>
       </dd>

      
     </dl>

    </p>
   </div>


   <div class="refsect1 changelog" id="refsect1-ref.pdo-mysql.connection-changelog">
    <h3 class="title">更新日志</h3>
    <p class="para">
     <table class="doctable informaltable">
      
       <thead>
        <tr>
         <th>版本</th>
         <th>说明</th>
        </tr>

       </thead>

       <tbody class="tbody">
        <tr>
         <td>5.3.6</td>
         <td>
          Prior to version 5.3.6, <em>charset</em> was ignored.
         </td>
        </tr>

       </tbody>
      
     </table>

    </p>
   </div>


   <div class="refsect1 examples" id="refsect1-ref.pdo-mysql.connection-examples">
    <h3 class="title">范例</h3>
    <p class="para">
     <div class="example" id="example-953">
      <p><strong>Example #2 PDO_MYSQL DSN examples</strong></p>
      <div class="example-contents"><p>
       The following example shows a PDO_MYSQL DSN for connecting to
       MySQL databases:
       <div class="example-contents">
<div class="cdata"><pre>
mysql:host=localhost;dbname=testdb
</pre></div>
       </div>

       More complete examples:
       <div class="example-contents">
<div class="cdata"><pre>
mysql:host=localhost;port=3307;dbname=testdb
mysql:unix_socket=/tmp/mysql.sock;dbname=testdb
</pre></div>
       </div>

      </p></div>
     </div>
    </p>
   </div>


   <div class="refsect1 notes" id="refsect1-ref.pdo-mysql.connection-notes">
    <h3 class="title">注释</h3>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <strong>Unix only:</strong><br />
     <p class="para">
      When the host name is set to <em>&quot;localhost&quot;</em>, then the connection to the 
      server is made thru a domain socket. If PDO_MYSQL is compiled against libmysqlclient then the 
      location of the socket file is at libmysqlclient&#039;s compiled in location. If PDO_MYSQL is compiled 
      against mysqlnd a default socket can be set thru the <a href="ref.pdo-mysql.html#ini.pdo-mysql.default-socket" class="link">
      pdo_mysql.default_socket</a> setting.
     </p>
    </p></blockquote>
   </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ref.pdo-mysql.html">MySQL (PDO)</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.pdo-sqlsrv.html">MS SQL Server (PDO)</a></div>
 <div class="up"><a href="ref.pdo-mysql.html">MySQL (PDO)</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
